<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <title>注册页</title>
    <style>

        .ht {
            text-align: center;
            color: cadetblue;
            font-family: 幼圆;
        }

        .tab {
            width: 500px;
            border: 5px solid cadetblue;
            margin: 0px auto;
            border-radius: 5px;
            font-family: 幼圆;
        }

        .ltr td {
            border: 1px solid powderblue;

        }

        .ipt {
            border: 0px;
            width: 50%;

        }

        .btn1 {
            border: 2px solid powderblue;
            border-radius: 4px;
            width: 60px;
            background-color: antiquewhite;

        }

        .msg {
            color: rgb(136, 209, 26);
        }

        .buttonContainer {
            text-align: center;
        }
    </style>
    <script>
        function checkUsername() {
            var usernameReg = /^[a-zA-Z0-9]{5,10}$/;
            var usernameInput = document.getElementById("usernameInput");
            var usernameMsg = document.getElementById("usernameMsg");
            var username = usernameInput.value;
            if (!usernameReg.test(username)) {
                usernameMsg.innerText = "用户名格式错误";
                return false;
            }

            // 用户名格式校验正确 继续校验用户名是否被占用
            // 获得 XMLHttpRequest 对象
            var request = new XMLHttpRequest();
            // 设置回调函数
            request.onreadystatechange = function () {
                if (4 === request.readyState && 200 === request.status) {
                    // if ("OK" === request.responseText) {
                    //     usernameMsg.innerText = "√";
                    //     return true;
                    // } else {
                    //     usernameMsg.innerText = "该用户名已被占用";
                    //     return false;
                    // }
                    var result = JSON.parse(request.responseText);
                    // console.log(request.responseText);
                    // console.log(result);
                    if (200 === result.code) {
                        usernameMsg.innerText = "√";
                        return true;
                    } else {
                        usernameMsg.innerText = "该用户名已被占用";
                        return false;
                    }
                }
            };
            // 设置请求发送的方式和目标资源
            request.open("GET", "/user/checkUsernameUsed?username=" + username);
            // 发送请求
            request.send();
        }

        function checkUserPwd() {
            var userPwdReg = /^\d{6}$/; // 6 位数字
            var userPwdInput = document.getElementById("userPwdInput");
            var userPwdMsg = document.getElementById("userPwdMsg");
            if (!userPwdReg.test(userPwdInput.value)) {
                userPwdMsg.innerText = "密码必须是6位数字";
                return false;
            }

            userPwdMsg.innerText = "√";
            return true;
        }

        function checkReUserPwd() {
            var reUserPwdMsg = document.getElementById("reUserPwdMsg");
            if (!checkUserPwd()) {
                reUserPwdMsg.innerText = "密码格式错误";
                return false;
            }
            var userPwdInput = document.getElementById("userPwdInput");
            var reUserPwdInput = document.getElementById("reUserPwdInput");
            if (!(userPwdInput.value === reUserPwdInput.value)) {
                reUserPwdMsg.innerText = "两次密码不一致";
                return false;
            }
            reUserPwdMsg.innerText = "√";
            return true;
        }

        function checkForm() {
            var flag = checkUsername() && checkReUserPwd();
            if (!flag) {
                window.alert("注册失败"); // 提示用户
                // location.reload(); // 刷新页面
            }
            return flag;
        }

        function clearCheckMsg() {
            var usernameMsg = document.getElementById("usernameMsg")
            var userPwdMsg = document.getElementById("userPwdMsg")
            var reUserPwdMsg = document.getElementById("reUserPwdMsg")
            usernameMsg.innerText = userPwdMsg.innerText = reUserPwdMsg.innerText = "";
        }
    </script>

</head>
<body>
<h1 class="ht">欢迎使用日程管理系统</h1>
<h3 class="ht">请注册</h3>
<form action="/user/register" method="post" onreset="clearCheckMsg()" onsubmit="return checkForm()">
    <table cellspacing="0px" class="tab">
        <tr class="ltr">
            <td>请输入账号</td>
            <td>
                <input class="ipt" id="usernameInput" name="username" onchange="checkUsername()" type="text">
                <span class="msg" id="usernameMsg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td>请输入密码</td>
            <td>
                <input class="ipt" id="userPwdInput" name="userPwd" onchange="checkUserPwd(), checkReUserPwd()"
                       type="password">
                <span class="msg" id="userPwdMsg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td>确认密码</td>
            <td>
                <input class="ipt" id="reUserPwdInput" onchange="checkReUserPwd()" type="password">
                <span class="msg" id="reUserPwdMsg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td class="buttonContainer" colspan="2">
                <input class="btn1" type="submit" value="注册">
                <input class="btn1" type="reset" value="重置">
                <button class="btn1"><a href="login.html">去登录</a></button>
            </td>
        </tr>
    </table>

</form>
</body>
</html>